package com.elite.controller;

import com.alibaba.fastjson.JSON;
import com.elite.entity.*;
import com.elite.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/2/21.
 */

@RestController
@RequestMapping("/express")
public class ExpressController {

    @Autowired
    private HttpSession session;
    @Autowired
    private GoodsorderService goodsorderService;
    @Autowired
    private FileService fileService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private SellerService sellerService;
    @Autowired
    private UserService userService;


    @RequestMapping(value = "/showExpress",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String showExpress(){

        List<Map<String,Object>> mapList = new ArrayList<Map<String, Object>>();
        User user = null;
        try {
            user = (User) session.getAttribute("loginUser");
        } catch (NullPointerException e){
            e.printStackTrace();
        }

        if (user != null){

            Integer userId = user.getUserId();
            List<Goodsorder> goodsorderList = null;
            try {
                goodsorderList = goodsorderService.selectByUserId(userId);
            } catch (NullPointerException e){
                e.printStackTrace();
            }

            if (goodsorderList != null) {
                for (Goodsorder goodsorder : goodsorderList){

                    Seller seller = sellerService.selectByPrimaryKey(goodsorder.getSellerId());
//                    User user1 = userService.selectById(seller.getUserId());
                    Goods goods = goodsService.selectByPrimaryKey(goodsorder.getGoodsId());

                    List<FileGoods> fileGoodses = null;

                    try{
                        fileGoodses = fileService.selectByGoodsId(goodsorder.getGoodsId());
                    } catch (NullPointerException e){
                        e.printStackTrace();
                    }

                    int index = 0;
                    if (fileGoodses != null && fileGoodses.size() != 0) {
                        index = (int) (Math.random()*fileGoodses.size());
                    }
                    FileGoods fileGoods = null;
                    if (fileGoodses != null && fileGoodses.size() != 0) {
                        fileGoods = fileGoodses.get(index);
                    }

                    File file = null;
                    if (fileGoods != null) {
                        file = fileService.selectByPrimaryKey(fileGoods.getFileId());
                    }

                    if (file != null){

                        Map<String,Object> map = new HashMap<String, Object>();
//                        map.put("userNikename",user1.getUserNikename());
                        map.put("fileUrl",file.getFileUrl());
                        map.put("goodsIntroducts",goods.getGoodsIntroducts());
                        map.put("goodsCity",goods.getGoodsCity());
                        map.put("goodsAdress",goodsorder.getGoodsAdress());
                        map.put("goodsorderCount",goodsorder.getGoodsorderCount());
                        map.put("goodsorderPrice",goodsorder.getGoodsorderPrice());
                        map.put("goodsorderId",goodsorder.getGoodsorderId());
                        mapList.add(map);
                    }

                }
            }

        }

        return JSON.toJSON(mapList).toString();
    }

    @RequestMapping(value = "/showExpressStatus",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String showExpressStatus(Integer goodsorderId){

        Goodsorder goodsorder = goodsorderService.selectByPrimaryKey(goodsorderId);

        return JSON.toJSON(goodsorder).toString();
    }

    @RequestMapping(value = "/ShowSellerUpdate",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String ShowSellerUpdate(){

        List<Map<String,Object>> mapList = new ArrayList<Map<String, Object>>();
        User user = (User) session.getAttribute("loginUser");
        Seller seller = sellerService.findByUserId(user.getUserId());

        if (seller != null) {

            List<Goodsorder> goodsorderList = null;
            try {
                goodsorderList = goodsorderService.selectBySellerId(seller.getSellerId());
            } catch (NullPointerException e) {
                e.printStackTrace();
            }

            if (goodsorderList != null) {
                for (Goodsorder goodsorder : goodsorderList) {

                    if (goodsorder.getGoodsorderStatus() == 0) {

                        goodsorder.setGoodsorderStatus(1);
                        goodsorder.setGoodsorderId(goodsorder.getGoodsorderId());
                        goodsorderService.updateStatus(goodsorder);
                    }
                    Goods goods = goodsService.selectByPrimaryKey(goodsorder.getGoodsId());

                    List<FileGoods> fileGoodses = null;

                    try {
                        fileGoodses = fileService.selectByGoodsId(goodsorder.getGoodsId());
                    } catch (NullPointerException e) {
                        e.printStackTrace();
                    }

                    int index = 0;
                    if (fileGoodses != null && fileGoodses.size() != 0) {
                        index = (int) (Math.random() * fileGoodses.size());
                    }
                    FileGoods fileGoods = null;
                    if (fileGoodses != null && fileGoodses.size() != 0) {
                        fileGoods = fileGoodses.get(index);
                    }

                    File file = null;
                    if (fileGoods != null) {
                        file = fileService.selectByPrimaryKey(fileGoods.getFileId());
                    }

                    if (file != null) {

                        Map<String, Object> map = new HashMap<String, Object>();
                        map.put("fileUrl", file.getFileUrl());
                        map.put("goodsName", goods.getGoodsName());
                        map.put("goodsCity", goods.getGoodsCity());
                        map.put("goodsPrice", goods.getGoodsPrice());
                        map.put("goodsorderId", goodsorder.getGoodsorderId());
                        map.put("goodsUnit", goods.getGoodsUnit());
                        map.put("goodsorderStatus", goodsorder.getGoodsorderStatus());
                        mapList.add(map);
                    }

                }
            }
        }


        return JSON.toJSON(mapList).toString();

    }

    @RequestMapping(value = "/sellerUpdate",method = RequestMethod.POST)
    @ResponseBody
    public void sellerUpdate(Integer goodsorderId,Integer goodsorderStatus){

        Goodsorder goodsorder = goodsorderService.selectByPrimaryKey(goodsorderId);
        if (goodsorder.getGoodsorderStatus() == 1){
            if (goodsorderStatus == 2){
                goodsorder.setGoodsorderId(goodsorderId);
                goodsorder.setGoodsorderStatus(goodsorderStatus);
                goodsorderService.updateStatus(goodsorder);
            }
        } else if (goodsorder.getGoodsorderStatus() == 2){
            if (goodsorderStatus == 3){
                goodsorder.setGoodsorderId(goodsorderId);
                goodsorder.setGoodsorderStatus(goodsorderStatus);
                goodsorderService.updateStatus(goodsorder);
            }
        }

    }

}
